﻿using System;
using System.ComponentModel;
using Loki;
using Loki.Commands;
using Luna.Model.Accounting;

namespace Luna.UI.Accounting
{
    public class AccountListViewModel : LunaViewModel
    {
        public LokiCollection<DisplayableAccount> Accounts { get; private set; }

        #region  SelectedAccountIndex
        private static PropertyChangedEventArgs _SelectedAccountIndexChangeArgs = ObservableHelper.CreateArgs<AccountListViewModel>(x => x.SelectedAccountIndex);

        private int? _SelectedAccountIndex;

        public int? SelectedAccountIndex
        {
            get
            {
                return _SelectedAccountIndex;
            }

            set
            {
                if (value != _SelectedAccountIndex)
                {
                    _SelectedAccountIndex = value;
                    NotifyChanged(_SelectedAccountIndexChangeArgs);
                }
            }
        }
        #endregion

        public AccountListViewModel()
        {
            Add = Commands.Create();
            Open = Commands.Create();
            Accounts = CreateCollection<DisplayableAccount>();

            Init += AccountListViewModel_Init;
            Load += AccountListViewModel_Load;
        }

        private void AccountListViewModel_Load(object P_Sender, EventArgs e)
        {
            Accounts.Synchronize<IAccount>(
                ApplicationModel.Accounting.Accounts,
                ConvertAccount,
                (i, d) => i.ID == d.ID,
                d => d.Name);
        }

        private void ConvertAccount(IAccount P_Source, DisplayableAccount P_Dest)
        {
            P_Dest.ID = P_Source.ID;
            P_Dest.Bank = P_Source.Bank;
            P_Dest.CurrentAmount = P_Source.CurrentAmount;
            P_Dest.ValidationDelta = P_Source.CurrentAmount - P_Source.ValidatedAmount;
            P_Dest.LastValidation = P_Source.ValidatedDate;
            P_Dest.Name = P_Source.Name;
        }

        private void AccountListViewModel_Init(object P_Sender, EventArgs e)
        {
            Commands.Handle(Add, Add_Execute);
            Commands.Handle(Open, Open_CanExecute, Open_Execute);
        }

        public ICommand Open { get; private set; }

        public ICommand Add { get; private set; }

        private void Add_Execute(object P_Sender, CommandEventArgs e)
        {
            LunaCommands.Application.LOAD.Execute(ApplicationModel.Accounting.Create<IEditableAccount>());
        }

        public void Open_CanExecute(object P_Sender, CanExecuteCommandEventArgs e)
        {
            e.CanExecute |= SelectedAccountIndex.HasValue;
        }

        public void Open_Execute(object P_Sender, CommandEventArgs e)
        {
            Guid L_SelectedAccountID = Accounts[SelectedAccountIndex.Value].ID;
            IAccount L_Account = ApplicationModel.Accounting.Accounts[L_SelectedAccountID];
            if (L_Account != null)
            {
                LunaCommands.Application.LOAD.Execute(L_Account);
            }
        }
    }
}
